package customplugin.wizards;

import java.net.URI;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;

import customplugin.projects.CustomProjectSupport;

import plugin.menumarble.Instrucciones;

public class CustomProjectNewWizard extends Wizard implements INewWizard, IExecutableExtension {


	private static final String WIZARD_NAME = NewWizardMessages.CustomProjectNewWizard_CustomProjectNewWizard_Custom_Plugin_Project;
	private static final String PAGE_NAME = "Custom Plug-in Project Wizard"; //$NON-NLS-1$
	private IConfigurationElement _configurationElement;

	private WizardNewProjectCreationPage _pageOne;
	
	public CustomProjectNewWizard() {
	    setWindowTitle(WIZARD_NAME);
	}



	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean performFinish() {
	    String name = _pageOne.getProjectName();
	    URI location = null;
	    if (!_pageOne.useDefaults()) {
	        location = _pageOne.getLocationURI();
	        System.err.println("location: " + location.toString()); //$NON-NLS-1$
	    } // else location == null
	    
	    CustomProjectSupport.createProject(name, location);
	    // Add this
	    BasicNewProjectResourceWizard.updatePerspective(_configurationElement);
	    
//        /**Mio -- PAGINA DE BIENVENIDA en vista**/
//	    IWorkbenchBrowserSupport support =PlatformUI.getWorkbench().getBrowserSupport();
//	    int style=IWorkbenchBrowserSupport.AS_VIEW;
//	    IWebBrowser browser = null;
//		try {
//			browser = support.createBrowser(style, "idBrowser", "Instrucciones MARBLE", "");
//		} catch (PartInitException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
//	    try {
//			browser.openURL(new URL("http://alarcos.inf-cr.uclm.es/"));
//			
//		} catch (PartInitException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (MalformedURLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		/**Mio**/
   
		BasicNewProjectResourceWizard.updatePerspective(_configurationElement);
				
	    return true;
	}


	
	@Override
	public void addPages() {
	    super.addPages();

	    _pageOne = new WizardNewProjectCreationPage(PAGE_NAME);

	    _pageOne.setTitle(NewWizardMessages.CustomProjectNewWizard_CustomProjectNewWizard_Create_something_custom);
	    _pageOne.setDescription(NewWizardMessages.CustomProjectNewWizard_2);

	    addPage(_pageOne);
	}



	@Override
	public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
	    _configurationElement = config;
	}



}
